/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.manager.model;

import com.tamnhat.entity.Partner;
import com.tamnhat.entity.Product;
import com.tamnhat.entity.ProductPartner;
import com.telsoft.applet.util.DateUtil;
import com.telsoft.applet.util.StringUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class ProductPartnerModel {

    public static boolean insert(Product p, String partnerId) throws Exception{
        Connection cn = null;
        PreparedStatement stmt = null;
        try {
            cn = AppServer.getConnection();
            String strSQL = " insert into sdp_product_partner values(seq_common.nextval," + partnerId + "," + p.getId() + ",to_date('" + StringUtil.format(p.getStartDate(), "dd-MM-yyyy") + "','dd-mm-yyyy'),to_date('" + StringUtil.format(p.getEndDate(), "dd-MM-yyyy") + "','dd-mm-yyyy')) ";
            if (p.getEndDate() == null) {
                strSQL = " insert into sdp_product_partner values(seq_common.nextval," + partnerId + "," + p.getId() + ",to_date('" + StringUtil.format(p.getStartDate(), "dd-MM-yyyy") + "','dd-mm-yyyy'),to_date('','dd-mm-yyyy')) ";
            }
            stmt = cn.prepareStatement(strSQL);
            int rs = stmt.executeUpdate();
            if (rs > 0) {
                return true;
            }
            return false;
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }

    public static boolean update(ProductPartner pp) throws Exception{
        Connection cn = null;
        PreparedStatement stmt = null;
        try {
            cn = AppServer.getConnection();
            String strSQL = " update sdp_product_partner "
                    + " set start_time = to_date('" + StringUtil.format(pp.getSDate(), "dd-MM-yyyy") + "','dd-mm-yyyy'), end_time = to_date('" + StringUtil.format(pp.getEDate(), "dd-MM-yyyy") + "','dd-mm-yyyy')"
                    + " where id = " + pp.getId() + " ";
            if (pp.getEDate() == null) {
                strSQL = " update sdp_product_partner "
                        + " set start_time = to_date('" + StringUtil.format(pp.getSDate(), "dd-MM-yyyy") + "','dd-mm-yyyy'), end_time = to_date('','dd-mm-yyyy')"
                        + " where id = " + pp.getId() + " ";
            }
            stmt = cn.prepareStatement(strSQL);
            int rs = stmt.executeUpdate();
            if (rs > 0) {
                return true;
            }
            return false;
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }

    public static boolean remove(String ppId) throws Exception{
        Connection cn = null;
        PreparedStatement stmt = null;
        try {
            cn = AppServer.getConnection();
            String strSQL = " delete from sdp_product_partner where id = " + ppId + " ";
            stmt = cn.prepareStatement(strSQL);
            int rs = stmt.executeUpdate();
            if (rs > 0) {
                return true;
            }
            return false;
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }
}
